home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / VideoToolbox 97.08.16 / VideoToolboxSources / BreakLines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-05  |  3.2 KB  |  134 lines  |  [TEXT/CWIE]

  1. /*
  2. BreakLines.c
  3.  
  4.     char *BreakLines(char *string,long lineLength);
  5. Takes a long C string and judiciously changes spaces to '\n' to yield lines
  6. shorter than lineLength, but words are never broken, even if they are longer
  7. than lineLength. All characters other than space and '\n' are treated as letters.
  8. E.g.    printf("%s\n",BreakLines(string,80));
  9.  
  10.     void PrintWrappedText(FILE *stream,const char *s);
  11. Prints out text, with word wrapping. Leaves the supplied string unmodified.
  12.  
  13.     void PrintWrappedTextToFile(const char *filename,const char *s);
  14. Appends wrapped text to a file.
  15.  
  16.     void PrintWrappedComment(FILE *stream,const char *s);
  17. Encloses the string s inside comment delimiters and prints it with word wrapping.
  18.  
  19.     void PrintWrappedCommentToFile(const char *filename,const char *s);
  20. Encloses the string s inside comment delimiters and appends it to a file,
  21. with word wrapping.
  22.  
  23. HISTORY:
  24. 1993    dgp    Wrote BreakLines.
  25. 9/7/93    dgp    Wrote PrintWrappedText and PrintWrappedTextToFile based on similar routines
  26.             called PrintComment, etc., written by dhb and jms.
  27. 9/11/93    dgp Added PrintWrappedComment
  28. 3/5/97    dgp NEWLINE is now always '\n'.
  29.         dhb    Got rid of NEWLINE and NL altogether.
  30. */
  31. #include "VideoToolbox.h"
  32.  
  33. char *BreakLines(char *string,long lineLength)
  34. {
  35.     long i,leftMargin,rightMargin,length;
  36.     int here;
  37.     
  38.     leftMargin=0;
  39.     length=strlen(string);
  40.     while(1){
  41.         rightMargin=leftMargin+lineLength;
  42.         if(rightMargin>=length)return string;        /* successful completion */
  43.         here=0;
  44.         if(!here)for(i=leftMargin;i<rightMargin;i++)if(string[i]=='\n'){
  45.             here=1;
  46.             break;
  47.         }
  48.         if(!here)for(;i>=leftMargin;i--)if(string[i]==' ' || string[i]=='\n'){
  49.             here=1;
  50.             break;
  51.         }
  52.         if(!here)for(i=leftMargin;i<length;i++)if(string[i]==' ' || string[i]=='\n'){
  53.             here=1;
  54.             break;
  55.         }
  56.         if(!here)return string;
  57.         string[i]='\n';
  58.         leftMargin=i+1;
  59.     }
  60. }
  61.  
  62. /*
  63. ROUTINE: PrintWrappedText
  64. PURPOSE:
  65.   Prints out text, with word wrapping. Leaves the supplied string unmodified.
  66. */
  67.  
  68. void PrintWrappedText(FILE *stream,const char *s)
  69. {
  70.     char *sTemp;
  71.     
  72.     sTemp=malloc(strlen(s)+1L);
  73.     if(sTemp==NULL)PrintfExit("PrintWrappedText: malloc(%ld) failed.\n"
  74.         ,strlen(s)+1L);
  75.     strcpy(sTemp,s);
  76.     BreakLines(sTemp,80);
  77.     fprintf(stream,"%s",sTemp);
  78.     free(sTemp);
  79. }
  80.  
  81. /*
  82. ROUTINE: PrintWrappedTextToFile
  83. PURPOSE:
  84.   Append wrapped text to a file.
  85. */
  86.  
  87. void PrintWrappedTextToFile(const char *filename,const char *s)
  88. {
  89.     FILE *stream;
  90.     
  91.     stream = fopen(filename,"a");
  92.     if (stream == NULL)
  93.         PrintfExit("PrintWrappedTextToFile: fopen(\"%s\",...) failed.",filename);
  94.     PrintWrappedText(stream,s);
  95.     fclose(stream);
  96. }
  97.  
  98. /*
  99. ROUTINE: PrintWrappedComment
  100. PURPOSE:
  101.   Prints out a comment, with word wrapping.
  102. */
  103.  
  104. void PrintWrappedComment(FILE *stream,const char *s)
  105. {
  106.     char *sTemp;
  107.     
  108.     sTemp=malloc(strlen(s)+6L);
  109.     if(sTemp==NULL)PrintfExit("PrintCommentString: malloc(%ld) failed.\n" 
  110.         ,strlen(s)+6L);
  111.     strcpy(sTemp,"/* ");
  112.     strcat(sTemp,s);
  113.     strcat(sTemp," */" "\n");
  114.     PrintWrappedText(stream,sTemp);
  115.     free(sTemp);
  116. }
  117.  
  118. /*
  119. ROUTINE: PrintWrappedCommentToFile
  120. PURPOSE:
  121.   Append wrapped comment to a file.
  122. */
  123.  
  124. void PrintWrappedCommentToFile(const char *filename,const char *s)
  125. {
  126.     FILE *stream;
  127.     
  128.     stream = fopen(filename,"a");
  129.     if (stream == NULL)
  130.         PrintfExit("PrintWrappedCommentToFile: fopen(\"%s\",...) failed.",filename);
  131.     PrintWrappedComment(stream,s);
  132.     fclose(stream);
  133. }
  134.